<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_t_pegawai extends CI_Model 
{
    function __construct()
    {
        parent::__construct();
 
		$this->load->database();

		// Paginaiton defaults
		$this->pagination_enabled = FALSE;
		$this->pagination_per_page = 10;
		$this->pagination_num_links = 5;
		$this->pager = '';

        /**
		 *    bool $this->raw_data		
		 *    Used to decide what data should the SQL queries retrieve if tables are joined
		 *     - TRUE:  just the field names of the t_pegawai table
		 *     - FALSE: related fields are replaced with the forign tables values
		 *    Triggered to TRUE in the controller/edit method		 
		 */
        $this->raw_data = FALSE;  
    }

	function get ( $id, $get_one = false )
	{
        
	    $select_statement = ( $this->raw_data ) ? 'NIP,NOMOR_INDUK,NM_PEGAWAI,INISIAL,KOTA_LAHIR,TANGGAL_LAHIR,KD_JENIS_KELAMIN,KD_GOL_DARAH,KD_AGAMA,KD_STATUS_NIKAH,ALAMAT,KD_POS,NO_TELP,KD_JENIS_KETENAGAAN,NO_HP,DIR_FOTO,USERNAME,TANGGAL_AKSES,NOMOR_INDUK_BARU,NUPTK,GELAR_DEPAN,GELAR_BELAKANG,NIY_NIGK,NIK,NM_IBU_KANDUNG,RT,RW,KELURAHAN_DESA,KECAMATAN,KABUPATEN_KOTA,PROPINSI,KD_AREA,EMAIL,STATUS_KEPEGAWAIAN,SERTIFIKASI_JABATAN,TAHUN_SERTIFIKAT_JABATAN,NOMOR_SERTIFIKAT_JABATAN,KD_SERTIFIKASI_BIDANG_STUDI,KD_PROGRAM_KEAHLIAN,LISENSI_KEPALA_SEKOLAH,CATATAN' : 'NIP,NOMOR_INDUK,NM_PEGAWAI,INISIAL,KOTA_LAHIR,TANGGAL_LAHIR,KD_JENIS_KELAMIN,KD_GOL_DARAH,KD_AGAMA,KD_STATUS_NIKAH,ALAMAT,KD_POS,NO_TELP,KD_JENIS_KETENAGAAN,NO_HP,DIR_FOTO,USERNAME,TANGGAL_AKSES,NOMOR_INDUK_BARU,NUPTK,GELAR_DEPAN,GELAR_BELAKANG,NIY_NIGK,NIK,NM_IBU_KANDUNG,RT,RW,KELURAHAN_DESA,KECAMATAN,KABUPATEN_KOTA,PROPINSI,KD_AREA,EMAIL,STATUS_KEPEGAWAIAN,SERTIFIKASI_JABATAN,TAHUN_SERTIFIKAT_JABATAN,NOMOR_SERTIFIKAT_JABATAN,KD_SERTIFIKASI_BIDANG_STUDI,KD_PROGRAM_KEAHLIAN,LISENSI_KEPALA_SEKOLAH,CATATAN';
		$this->db->select( $select_statement );
		$this->db->from('t_pegawai');
        

		// Pick one record
		// Field order sample may be empty because no record is requested, eg. create/GET event
		if( $get_one )
        {
            $this->db->limit(1,0);
        }
		else // Select the desired record
        {
            $this->db->where( 'NIP', $id );
        }

		$query = $this->db->get();

		if ( $query->num_rows() > 0 )
		{
			$row = $query->row_array();
			return array( 
	'NIP' => $row['NIP'],
	'NOMOR_INDUK' => $row['NOMOR_INDUK'],
	'NM_PEGAWAI' => $row['NM_PEGAWAI'],
	'INISIAL' => $row['INISIAL'],
	'KOTA_LAHIR' => $row['KOTA_LAHIR'],
	'TANGGAL_LAHIR' => $row['TANGGAL_LAHIR'],
	'KD_JENIS_KELAMIN' => $row['KD_JENIS_KELAMIN'],
	'KD_GOL_DARAH' => $row['KD_GOL_DARAH'],
	'KD_AGAMA' => $row['KD_AGAMA'],
	'KD_STATUS_NIKAH' => $row['KD_STATUS_NIKAH'],
	'ALAMAT' => $row['ALAMAT'],
	'KD_POS' => $row['KD_POS'],
	'NO_TELP' => $row['NO_TELP'],
	'KD_JENIS_KETENAGAAN' => $row['KD_JENIS_KETENAGAAN'],
	'NO_HP' => $row['NO_HP'],
	'DIR_FOTO' => $row['DIR_FOTO'],
	'USERNAME' => $row['USERNAME'],
	'TANGGAL_AKSES' => $row['TANGGAL_AKSES'],
	'NOMOR_INDUK_BARU' => $row['NOMOR_INDUK_BARU'],
	'NUPTK' => $row['NUPTK'],
	'GELAR_DEPAN' => $row['GELAR_DEPAN'],
	'GELAR_BELAKANG' => $row['GELAR_BELAKANG'],
	'NIY_NIGK' => $row['NIY_NIGK'],
	'NIK' => $row['NIK'],
	'NM_IBU_KANDUNG' => $row['NM_IBU_KANDUNG'],
	'RT' => $row['RT'],
	'RW' => $row['RW'],
	'KELURAHAN_DESA' => $row['KELURAHAN_DESA'],
	'KECAMATAN' => $row['KECAMATAN'],
	'KABUPATEN_KOTA' => $row['KABUPATEN_KOTA'],
	'PROPINSI' => $row['PROPINSI'],
	'KD_AREA' => $row['KD_AREA'],
	'EMAIL' => $row['EMAIL'],
	'STATUS_KEPEGAWAIAN' => $row['STATUS_KEPEGAWAIAN'],
	'SERTIFIKASI_JABATAN' => $row['SERTIFIKASI_JABATAN'],
	'TAHUN_SERTIFIKAT_JABATAN' => $row['TAHUN_SERTIFIKAT_JABATAN'],
	'NOMOR_SERTIFIKAT_JABATAN' => $row['NOMOR_SERTIFIKAT_JABATAN'],
	'KD_SERTIFIKASI_BIDANG_STUDI' => $row['KD_SERTIFIKASI_BIDANG_STUDI'],
	'KD_PROGRAM_KEAHLIAN' => $row['KD_PROGRAM_KEAHLIAN'],
	'LISENSI_KEPALA_SEKOLAH' => $row['LISENSI_KEPALA_SEKOLAH'],
	'CATATAN' => $row['CATATAN'],
 );
		}
        else
        {
            return array();
        }
	}



	function insert ( $data )
	{
		$this->db->insert( 't_pegawai', $data );
		return $this->db->insert_id();
	}
	


	function update ( $id, $data )
	{
		$this->db->where( 'NIP', $id );
		$this->db->update( 't_pegawai', $data );
	}


	
	function delete ( $id )
	{
        if( is_array( $id ) )
        {
            $this->db->where_in( 'NIP', $id );            
        }
        else
        {
            $this->db->where( 'NIP', $id );
        }
        $this->db->delete( 't_pegawai' );
        
	}



	function lister ( $page = FALSE )
	{
        
	    $this->db->start_cache();
		$this->db->select( 'NIP,NOMOR_INDUK,NM_PEGAWAI,INISIAL,KOTA_LAHIR,TANGGAL_LAHIR,KD_JENIS_KELAMIN,KD_GOL_DARAH,KD_AGAMA,KD_STATUS_NIKAH,ALAMAT,KD_POS,NO_TELP,KD_JENIS_KETENAGAAN,NO_HP,DIR_FOTO,USERNAME,TANGGAL_AKSES,NOMOR_INDUK_BARU,NUPTK,GELAR_DEPAN,GELAR_BELAKANG,NIY_NIGK,NIK,NM_IBU_KANDUNG,RT,RW,KELURAHAN_DESA,KECAMATAN,KABUPATEN_KOTA,PROPINSI,KD_AREA,EMAIL,STATUS_KEPEGAWAIAN,SERTIFIKASI_JABATAN,TAHUN_SERTIFIKAT_JABATAN,NOMOR_SERTIFIKAT_JABATAN,KD_SERTIFIKASI_BIDANG_STUDI,KD_PROGRAM_KEAHLIAN,LISENSI_KEPALA_SEKOLAH,CATATAN');
		$this->db->from( 't_pegawai' );
		//$this->db->order_by( '', 'ASC' );
        

        /**
         *   PAGINATION
         */
        if( $this->pagination_enabled == TRUE )
        {
            $config = array();
            $config['total_rows']  = $this->db->count_all_results('t_pegawai');
            $config['base_url']    = 't_pegawai/index/';
            $config['uri_segment'] = 3;
            $config['cur_tag_open'] = '<span class="current">';
            $config['cur_tag_close'] = '</span>';
            $config['per_page']    = $this->pagination_per_page;
            $config['num_links']   = $this->pagination_num_links;

            $this->load->library('pagination');
            $this->pagination->initialize($config);
            $this->pager = $this->pagination->create_links();
    
            $this->db->limit( $config['per_page'], $page );
        }

        // Get the results
		$query = $this->db->get();
		
		$temp_result = array();

		foreach ( $query->result_array() as $row )
		{
			$temp_result[] = array( 
	'NIP' => $row['NIP'],
	'NOMOR_INDUK' => $row['NOMOR_INDUK'],
	'NM_PEGAWAI' => $row['NM_PEGAWAI'],
	'INISIAL' => $row['INISIAL'],
	'KOTA_LAHIR' => $row['KOTA_LAHIR'],
	'TANGGAL_LAHIR' => $row['TANGGAL_LAHIR'],
	'KD_JENIS_KELAMIN' => $row['KD_JENIS_KELAMIN'],
	'KD_GOL_DARAH' => $row['KD_GOL_DARAH'],
	'KD_AGAMA' => $row['KD_AGAMA'],
	'KD_STATUS_NIKAH' => $row['KD_STATUS_NIKAH'],
	'ALAMAT' => $row['ALAMAT'],
	'KD_POS' => $row['KD_POS'],
	'NO_TELP' => $row['NO_TELP'],
	'KD_JENIS_KETENAGAAN' => $row['KD_JENIS_KETENAGAAN'],
	'NO_HP' => $row['NO_HP'],
	'DIR_FOTO' => $row['DIR_FOTO'],
	'USERNAME' => $row['USERNAME'],
	'TANGGAL_AKSES' => $row['TANGGAL_AKSES'],
	'NOMOR_INDUK_BARU' => $row['NOMOR_INDUK_BARU'],
	'NUPTK' => $row['NUPTK'],
	'GELAR_DEPAN' => $row['GELAR_DEPAN'],
	'GELAR_BELAKANG' => $row['GELAR_BELAKANG'],
	'NIY_NIGK' => $row['NIY_NIGK'],
	'NIK' => $row['NIK'],
	'NM_IBU_KANDUNG' => $row['NM_IBU_KANDUNG'],
	'RT' => $row['RT'],
	'RW' => $row['RW'],
	'KELURAHAN_DESA' => $row['KELURAHAN_DESA'],
	'KECAMATAN' => $row['KECAMATAN'],
	'KABUPATEN_KOTA' => $row['KABUPATEN_KOTA'],
	'PROPINSI' => $row['PROPINSI'],
	'KD_AREA' => $row['KD_AREA'],
	'EMAIL' => $row['EMAIL'],
	'STATUS_KEPEGAWAIAN' => $row['STATUS_KEPEGAWAIAN'],
	'SERTIFIKASI_JABATAN' => $row['SERTIFIKASI_JABATAN'],
	'TAHUN_SERTIFIKAT_JABATAN' => $row['TAHUN_SERTIFIKAT_JABATAN'],
	'NOMOR_SERTIFIKAT_JABATAN' => $row['NOMOR_SERTIFIKAT_JABATAN'],
	'KD_SERTIFIKASI_BIDANG_STUDI' => $row['KD_SERTIFIKASI_BIDANG_STUDI'],
	'KD_PROGRAM_KEAHLIAN' => $row['KD_PROGRAM_KEAHLIAN'],
	'LISENSI_KEPALA_SEKOLAH' => $row['LISENSI_KEPALA_SEKOLAH'],
	'CATATAN' => $row['CATATAN'],
 );
		}
        $this->db->flush_cache(); 
		return $temp_result;
	}



	function search ( $keyword, $page = FALSE )
	{
	    $meta = $this->metadata();
	    $this->db->start_cache();
		$this->db->select( 'NIP,NOMOR_INDUK,NM_PEGAWAI,INISIAL,KOTA_LAHIR,TANGGAL_LAHIR,KD_JENIS_KELAMIN,KD_GOL_DARAH,KD_AGAMA,KD_STATUS_NIKAH,ALAMAT,KD_POS,NO_TELP,KD_JENIS_KETENAGAAN,NO_HP,DIR_FOTO,USERNAME,TANGGAL_AKSES,NOMOR_INDUK_BARU,NUPTK,GELAR_DEPAN,GELAR_BELAKANG,NIY_NIGK,NIK,NM_IBU_KANDUNG,RT,RW,KELURAHAN_DESA,KECAMATAN,KABUPATEN_KOTA,PROPINSI,KD_AREA,EMAIL,STATUS_KEPEGAWAIAN,SERTIFIKASI_JABATAN,TAHUN_SERTIFIKAT_JABATAN,NOMOR_SERTIFIKAT_JABATAN,KD_SERTIFIKASI_BIDANG_STUDI,KD_PROGRAM_KEAHLIAN,LISENSI_KEPALA_SEKOLAH,CATATAN');
		$this->db->from( 't_pegawai' );
        

		// Delete this line after setting up the search conditions 
        die('Please see models/model_t_pegawai.php for setting up the search method.');
		
        /**
         *  Rename field_name_to_search to the field you wish to search 
         *  or create advanced search conditions here
		 */
        $this->db->where( 'field_name_to_search LIKE "%'.$keyword.'%"' );

        /**
         *   PAGINATION
         */
        if( $this->pagination_enabled == TRUE )
        {
            $config = array();
            $config['total_rows']  = $this->db->count_all_results('t_pegawai');
            $config['base_url']    = '/t_pegawai/search/'.$keyword.'/';
            $config['uri_segment'] = 4;
            $config['per_page']    = $this->pagination_per_page;
            $config['num_links']   = $this->pagination_num_links;
    
            $this->load->library('pagination');
            $this->pagination->initialize($config);
            $this->pager = $this->pagination->create_links();
    
            $this->db->limit( $config['per_page'], $page );
        }

		$query = $this->db->get();

		$temp_result = array();

		foreach ( $query->result_array() as $row )
		{
			$temp_result[] = array( 
	'NIP' => $row['NIP'],
	'NOMOR_INDUK' => $row['NOMOR_INDUK'],
	'NM_PEGAWAI' => $row['NM_PEGAWAI'],
	'INISIAL' => $row['INISIAL'],
	'KOTA_LAHIR' => $row['KOTA_LAHIR'],
	'TANGGAL_LAHIR' => $row['TANGGAL_LAHIR'],
	'KD_JENIS_KELAMIN' => $row['KD_JENIS_KELAMIN'],
	'KD_GOL_DARAH' => $row['KD_GOL_DARAH'],
	'KD_AGAMA' => $row['KD_AGAMA'],
	'KD_STATUS_NIKAH' => $row['KD_STATUS_NIKAH'],
	'ALAMAT' => $row['ALAMAT'],
	'KD_POS' => $row['KD_POS'],
	'NO_TELP' => $row['NO_TELP'],
	'KD_JENIS_KETENAGAAN' => $row['KD_JENIS_KETENAGAAN'],
	'NO_HP' => $row['NO_HP'],
	'DIR_FOTO' => $row['DIR_FOTO'],
	'USERNAME' => $row['USERNAME'],
	'TANGGAL_AKSES' => $row['TANGGAL_AKSES'],
	'NOMOR_INDUK_BARU' => $row['NOMOR_INDUK_BARU'],
	'NUPTK' => $row['NUPTK'],
	'GELAR_DEPAN' => $row['GELAR_DEPAN'],
	'GELAR_BELAKANG' => $row['GELAR_BELAKANG'],
	'NIY_NIGK' => $row['NIY_NIGK'],
	'NIK' => $row['NIK'],
	'NM_IBU_KANDUNG' => $row['NM_IBU_KANDUNG'],
	'RT' => $row['RT'],
	'RW' => $row['RW'],
	'KELURAHAN_DESA' => $row['KELURAHAN_DESA'],
	'KECAMATAN' => $row['KECAMATAN'],
	'KABUPATEN_KOTA' => $row['KABUPATEN_KOTA'],
	'PROPINSI' => $row['PROPINSI'],
	'KD_AREA' => $row['KD_AREA'],
	'EMAIL' => $row['EMAIL'],
	'STATUS_KEPEGAWAIAN' => $row['STATUS_KEPEGAWAIAN'],
	'SERTIFIKASI_JABATAN' => $row['SERTIFIKASI_JABATAN'],
	'TAHUN_SERTIFIKAT_JABATAN' => $row['TAHUN_SERTIFIKAT_JABATAN'],
	'NOMOR_SERTIFIKAT_JABATAN' => $row['NOMOR_SERTIFIKAT_JABATAN'],
	'KD_SERTIFIKASI_BIDANG_STUDI' => $row['KD_SERTIFIKASI_BIDANG_STUDI'],
	'KD_PROGRAM_KEAHLIAN' => $row['KD_PROGRAM_KEAHLIAN'],
	'LISENSI_KEPALA_SEKOLAH' => $row['LISENSI_KEPALA_SEKOLAH'],
	'CATATAN' => $row['CATATAN'],
 );
		}
        $this->db->flush_cache(); 
		return $temp_result;
	}





    /**
     *  Some utility methods
     */
    function fields( $withID = FALSE )
    {
        $fs = array(
	'NIP' => lang('NIP'),
	'NOMOR_INDUK' => lang('NOMOR_INDUK'),
	'NM_PEGAWAI' => lang('NM_PEGAWAI'),
	'INISIAL' => lang('INISIAL'),
	'KOTA_LAHIR' => lang('KOTA_LAHIR'),
	'TANGGAL_LAHIR' => lang('TANGGAL_LAHIR'),
	'KD_JENIS_KELAMIN' => lang('KD_JENIS_KELAMIN'),
	'KD_GOL_DARAH' => lang('KD_GOL_DARAH'),
	'KD_AGAMA' => lang('KD_AGAMA'),
	'KD_STATUS_NIKAH' => lang('KD_STATUS_NIKAH'),
	'ALAMAT' => lang('ALAMAT'),
	'KD_POS' => lang('KD_POS'),
	'NO_TELP' => lang('NO_TELP'),
	'KD_JENIS_KETENAGAAN' => lang('KD_JENIS_KETENAGAAN'),
	'NO_HP' => lang('NO_HP'),
	'DIR_FOTO' => lang('DIR_FOTO'),
	'USERNAME' => lang('USERNAME'),
	'TANGGAL_AKSES' => lang('TANGGAL_AKSES'),
	'NOMOR_INDUK_BARU' => lang('NOMOR_INDUK_BARU'),
	'NUPTK' => lang('NUPTK'),
	'GELAR_DEPAN' => lang('GELAR_DEPAN'),
	'GELAR_BELAKANG' => lang('GELAR_BELAKANG'),
	'NIY_NIGK' => lang('NIY_NIGK'),
	'NIK' => lang('NIK'),
	'NM_IBU_KANDUNG' => lang('NM_IBU_KANDUNG'),
	'RT' => lang('RT'),
	'RW' => lang('RW'),
	'KELURAHAN_DESA' => lang('KELURAHAN_DESA'),
	'KECAMATAN' => lang('KECAMATAN'),
	'KABUPATEN_KOTA' => lang('KABUPATEN_KOTA'),
	'PROPINSI' => lang('PROPINSI'),
	'KD_AREA' => lang('KD_AREA'),
	'EMAIL' => lang('EMAIL'),
	'STATUS_KEPEGAWAIAN' => lang('STATUS_KEPEGAWAIAN'),
	'SERTIFIKASI_JABATAN' => lang('SERTIFIKASI_JABATAN'),
	'TAHUN_SERTIFIKAT_JABATAN' => lang('TAHUN_SERTIFIKAT_JABATAN'),
	'NOMOR_SERTIFIKAT_JABATAN' => lang('NOMOR_SERTIFIKAT_JABATAN'),
	'KD_SERTIFIKASI_BIDANG_STUDI' => lang('KD_SERTIFIKASI_BIDANG_STUDI'),
	'KD_PROGRAM_KEAHLIAN' => lang('KD_PROGRAM_KEAHLIAN'),
	'LISENSI_KEPALA_SEKOLAH' => lang('LISENSI_KEPALA_SEKOLAH'),
	'CATATAN' => lang('CATATAN')
);

        if( $withID == FALSE )
        {
            unset( $fs[0] );
        }
        return $fs;
    }  
    


    function pagination( $bool )
    {
        $this->pagination_enabled = ( $bool === TRUE ) ? TRUE : FALSE;
    }



    /**
     *  Parses the table data and look for enum values, to match them with language variables
     */             
    function metadata()
    {
        $this->load->library('explain_table');

        $metadata = $this->explain_table->parse( 't_pegawai' );

        foreach( $metadata as $k => $md )
        {
            if( !empty( $md['enum_values'] ) )
            {
                $metadata[ $k ]['enum_names'] = array_map( 'lang', $md['enum_values'] );                
            } 
        }
        return $metadata; 
    }
}
